/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package database;

/**
 *
 * @author User
 */
import entity.Department;
import iface.IManagerDB;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import manager.DepartmentManager;
import manager.DisabledDepartmentManager;

public class DepartmentDB implements IManagerDB<DepartmentManager, DisabledDepartmentManager> {

    private CallableStatement cstmt;
    private java.sql.Connection conn;
    private ResultSet rs;

    public DepartmentDB(Connection conn) {
        this.conn = conn;
    }

    @Override
    public void loadData(DepartmentManager t, boolean value) {
        try {
            cstmt = conn.prepareCall("{call sp_GetDepartment(?)}");
            cstmt.setBoolean(1, value);
            rs = cstmt.executeQuery();
            while (rs.next()) {
                int departmentID = rs.getInt("DepartmentID");
                String departmentName = rs.getString("DepartmentName");
                boolean status = rs.getBoolean("Status");
                Department department = new Department(departmentID, departmentName, status);
                t.add(department);
            }
        } catch (SQLException ex) {
            Logger.getLogger(DepartmentDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void insert(Object object) {
        Department department = (Department) (object);

        try {
            cstmt = conn.prepareCall("{call sp_InsertDepartment(?,?,?)}");
            cstmt.setInt(1, department.getDepartmentID());
            cstmt.setString(2, department.getDepartmentName());
            cstmt.setBoolean(3, department.isStatus());

            cstmt.executeUpdate();
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(DepartmentDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void update(Object object) {
        Department department = (Department) (object);

        try {
            cstmt = conn.prepareCall("{call sp_UpdateDepartment(?,?,?)}");
            cstmt.setInt(1, department.getDepartmentID());
            cstmt.setString(2, department.getDepartmentName());
            cstmt.setBoolean(3, department.isStatus());

            cstmt.executeUpdate();
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(DepartmentDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void delete(Object object) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
